﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using System.IO;

namespace TS_XY_Train_Repaire.Main_Base.DigitalMap
{
    public partial class Main_Base_DigitalMap_TrainType_Days_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Main_Base_DigitalMap_TrainType_Days_ManaForm()
        {
            InitializeComponent();
        }

        private void Main_Base_DigitalMap_TrainType_Days_ManaForm_Load(object sender, EventArgs e)
        {
            _Load_tl_Main();
        }

        private void Main_Base_DigitalMap_TrainType_Days_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        private void _Load_tl_Main()
        {
            LeverString_SysTreeMainList _DL = new LeverString_SysTreeMainList();
            ArrayList pList = new ArrayList();
            string strSQL = "";
            strSQL = "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_id in (select f_ttid from TS_TR_PS_Work_Main where f_del=0)";//机型
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _DL = new LeverString_SysTreeMainList();
                _DL.f_id = "T" + read["f_id"].ToString();
                _DL.f_name = read["f_name"].ToString();
                pList.Add(_DL);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_ttid from TS_TR_PS_Work_Main where f_del=0 ";//整备流程
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _DL = new LeverString_SysTreeMainList();
                _DL.f_id = "M" + read["f_id"].ToString();
                _DL.f_name = read["f_name"].ToString();
                _DL.f_parent = "T" + read["f_ttid"].ToString();
                pList.Add(_DL);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_mwid,(select count(*) from TS_S_B_Digital_Map_TrainType_Steps where f_del=0 and f_mwsid=t.f_id) as f_type from TS_TR_PS_Work_Main_Day as t where f_del=0 and f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) order by f_order asc";//整备作业流程
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _DL = new LeverString_SysTreeMainList();
                _DL.f_id = read["f_id"].ToString();
                _DL.f_name = read["f_name"].ToString();
                _DL.f_type = read["f_type"].ToString();
                _DL.f_parent = "M" + read["f_mwid"].ToString();
                pList.Add(_DL);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Main.DataSource = pList;
            tl_Main.ExpandAll();
        }

        private void bT_liulan_Click(object sender, EventArgs e)
        {
            ofd_img.Filter = "图片文件|*.jpg|图片文件|*.png";
            if (ofd_img.ShowDialog() == DialogResult.OK)
            {
                if (ofd_img.FileName != "")
                {
                    pB_img.ImageLocation = ofd_img.FileName;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (ofd_img.FileName.Length > 0 && tl_Main.FocusedNode.GetValue("f_id").ToString().Substring(0, 1) != "T" && tl_Main.FocusedNode.GetValue("f_id").ToString().Substring(0, 1) != "M")
                {
                    string sql = "";
                    FileStream fs = new FileStream(ofd_img.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                    string base64String = Convert.ToBase64String(bt);
                    br.Close();
                    fs.Close();
                    string gk = pB_img.Height.ToString() + "," + pB_img.Width.ToString();
                    if (TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_B_Digital_Map_TrainType_Steps where f_del=0 and f_mwsid=" + tl_Main.FocusedNode.GetValue("f_id").ToString(), "f_count") == "0")
                    {
                        sql = "insert into TS_S_B_Digital_Map_TrainType_Steps (f_brief,f_size,f_workerid) values('" + base64String + "','" + gk + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                    }
                    else
                    {
                        sql = "update TS_S_B_Digital_Map_TrainType_Steps set f_brief='" + base64String + "',f_size='" + gk + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_mwsid=" + tl_Main.FocusedNode.GetValue("f_id").ToString();
                    } TS_Win_Class.MySqlCom(sql);
                }
                else
                {
                    MessageBox.Show("无效的流程节点或者图片为空");
                }
            }
            catch
            {
                MessageBox.Show("无效的流程节点或者图片为空");
            }
        }

        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (tl_Main.FocusedNode.GetValue("f_id").ToString().Substring(0, 1) != "T" && tl_Main.FocusedNode.GetValue("f_id").ToString().Substring(0, 1) != "M")
                {
                    MemoryStream ms = new MemoryStream(Convert.FromBase64String(TS_Win_Class.GetSQlFieldValue("select top 1 f_brief from  TS_S_B_Digital_Map_TrainType_Steps where f_del=0 and f_mwsid=" + tl_Main.FocusedNode.GetValue("f_id").ToString(), "f_brief")));
                    Image image = Image.FromStream(ms, true);
                    pB_img.Image = image;
                }
                else
                {
                    pB_img.Image = null;
                }
            }
            catch
            {
                pB_img.Image = null;
            }
        }
    }
}